<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="120px">
                <el-form-item label="通道" prop="tabstatus">
                    <el-select v-model="queryParams.tabstatus" placeholder="请选择通道" style="width: 240px">
                        <el-option v-for="item in tabstatus" :key="item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <!-- 店铺 -->
                <el-form-item label="店铺" prop="shop_id">
                    <el-select v-model="queryParams.shop_id" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="争议时间" prop="customDate">
                    <el-radio-group v-model="queryParams.customDate" @change="changeCustomDate">
                        <el-radio-button :label="1">今天</el-radio-button>
                        <el-radio-button :label="2">昨天</el-radio-button>
                        <el-radio-button :label="3">近3天</el-radio-button>
                        <el-radio-button :label="4">近7天</el-radio-button>
                        <el-radio-button :label="5">近30天</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <!-- <el-form-item label="争议月份" prop="date">
                    <el-date-picker type="monthrange" v-model="queryParams.date" start-placeholder="开始时间" end-placeholder="结束时间"></el-date-picker>
                </el-form-item> -->
                <el-form-item label="付款时间" prop="createtime">
                    <el-date-picker v-model="queryParams.createtime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="争议时间" prop="create_time">
                    <el-date-picker v-model="queryParams.create_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="争议状态" prop="status">
                    <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 240px">
                        <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="匹配状态" prop="type">
                    <el-select v-model="queryParams.type" placeholder="请选择匹配状态" clearable style="width: 240px">
                        <el-option v-for="item in types" :key="item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="订单号" prop="order_name">
                    <el-input v-model.trim="queryParams.order_name" placeholder="请输入订单号" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="物流单号" prop="tracking_number">
                    <el-input v-model.trim="queryParams.tracking_number" placeholder="请输入物流单号" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card"  v-loading="loading">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="primary" plain @click="StoreDisputeRate">店铺争议率</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain @click="UnmatchedDispute">未匹配争议</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-row :gutter="10" class="total-warp">
                <el-col :md="4" :sm="24">
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">总计</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ spends_Total }} </span>
                        </div>
                    </el-card>
                </el-col>
                <el-col :md="4" :sm="24" v-for="(item, index) in spends" :key="index">
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">{{ item.platform }}</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ item.count }}</span>
                        </div>
                    </el-card>
                </el-col>
            </el-row>
            <!-- <el-tabs v-model="activeTab" @tab-click="handleStatus" type="border-card">
                <el-tab-pane v-for="(item, index) in tabstatus" :key="index" :label="item.label" :name="item.value.toString()"> </el-tab-pane>
            </el-tabs> -->
            <el-table v-loading="loading" :data="tableData" row-key="id" border script>
                <el-table-column label="订单ID" prop="order_id" min-width="140"></el-table-column>
                <el-table-column label="渠道" prop="tabstatus" min-width="80"></el-table-column>
                <el-table-column label="争议时间" prop="time" min-width="100"></el-table-column>
                <el-table-column label="店铺" prop="shop_name" min-width="100"></el-table-column>
                <el-table-column label="争议状态" prop="status" min-width="80"></el-table-column>
                <!-- <el-table-column label="争议原因" prop="reason" min-width="160"></el-table-column> -->
                <el-table-column label="争议原因" prop="reason" min-width="160">
                    <template slot-scope="scope">
                        <p>{{ scope.row.reason }}</p>
                        <el-link type="primary" @click="ReasonForDisputeDialog(scope.row)">争议原因</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="订单号" prop="order_name">
                    <template slot-scope="scope">
                        <el-link type="primary" @click="showDetailDialog(scope.row)">{{ scope.row.order_name }}</el-link>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="支付状态" prop="financial_status" min-width="160"></el-table-column> -->
                <el-table-column label="付款时间" prop="createtime" min-width="100"></el-table-column>
                <el-table-column label="发货状态" prop="fulfillment_status" min-width="100"></el-table-column>
                <el-table-column label="发货时间" prop="OrderDeliveryTime" min-width="100"></el-table-column>
                <el-table-column label="物流单号" prop="tracking_number" min-width="100"></el-table-column>
                <el-table-column label="签收时间" prop="SignInTime" min-width="100"></el-table-column>
                <el-table-column label="会话数量" prop="conversations">
                    <template slot-scope="scope">
                        <el-link type="primary" @click="showConversaDialog(scope.row)">{{ scope.row.conversations }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="邮箱" prop="email" min-width="160"></el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList" />
                
            <DetailDialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" :type="detailDialog.type" @success="handleQuery"></DetailDialog>
            <DisputeDialog v-model="disputeDialog.visible" v-if="disputeDialog.visible" :params="disputeDialog.data" :type="disputeDialog.type" @success="handleQuery"></DisputeDialog>
            <ConversaDialog v-model="conversaDialog.visible" v-if="conversaDialog.visible" :params="conversaDialog.data" :type="conversaDialog.type" @success="handleQuery"></ConversaDialog>
            <SpuSalesRankingDialog v-model="rankDialog.visible" v-if="rankDialog.visible" :params="rankDialog.data"></SpuSalesRankingDialog>
            <UnmatchedsDialog v-model="UnmatchedDialog.visible" v-if="UnmatchedDialog.visible" :params="UnmatchedDialog.data"></UnmatchedsDialog>
        </div>
    </div>
</template>
<script>
import DetailDialog from '../components/DetailDialog.vue'
import DisputeDialog from '../components/DisputeDialog.vue'
import ConversaDialog from '../components/ConversaDialog.vue'
import SpuSalesRankingDialog from '../components/SpuSalesRankingDialog.vue'
import UnmatchedsDialog from '../components/UnmatchedsDialog.vue'
import { getShopList } from '@/api/shop'
import { queryCheckDisputeList,exportCheckDispute } from '@/api/order'
import { exportFile } from '@/utils/index'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'OrderCheckDispute',
    components: {DetailDialog,ConversaDialog,DisputeDialog,SpuSalesRankingDialog,UnmatchedsDialog},
    data() {
        return {
            rankDialog: {
                visible: false,
                data: {}
            },
            UnmatchedDialog: {
                visible: false,
                data: {}
            },
            action: process.env.VUE_APP_BASE_API + '/order/OrderCheckDispute/import',
            downloadUrl: '',
            // 遮罩层
            loading: true,
            download: false,
            // 选中数组
            ids: [],
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 状态数据字典
            types:[
                { label: '全部', value: '' },
                { label: '匹配成功', value: 'MatchSuccessful' },
                { label: '等待匹配', value: 'WaitingForAMatch' },
            ],
            status:[
                { label: '待处理', value: '待处理' },
                { label: '审核中', value: '审核中' },
                { label: '争议赢', value: '争议赢' },
                { label: '争议输', value: '争议输' },
                { label: '其他', value: '其他' },
            ],
            tabstatus: [
                { label: '所有通道', value: "All" },
                { label: 'shopifyPayment', value: "shopify" },
                { label: 'afterpay', value: "afterpay" },
                { label: 'airwallex', value: "airwallex" },
                { label: 'paypal', value: "paypal" },
                { label: 'stripe', value: "stripe" },
            ],
            activeTab: 'shopify',
            shopList: [],
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                shop_id: undefined,
                type: undefined,
                status: undefined,
                tabstatus: 'shopify',
                customDate: undefined,
                date: [],
            },
            spends_Total: null,
            spends: [],
            detailDialog: {
                visible: false,
                data: {},
                type: ''
            },
            disputeDialog: {
                visible: false,
                data: {},
                type: ''
            },
            conversaDialog: {
                visible: false,
                data: {},
                type: ''
            },
        }
    },
    watch: {
        'queryParams.date': {
            handler(val) {
                if (val?.length == 2) {
                    this.queryParams.customDate = 0
                }
            },
            immediate: true,
            deep: true,
        },
    },
    async created() {
        this.loading = true
        try {
            const [shopData] = await Promise.all([getShopList()])
            this.shopList = shopData?.data?.list ?? []
        } catch (error) {
            this.loading = false
            console.log(error)
        }
        this.getList()
    },
    methods: {
        
        StoreDisputeRate() {
            this.rankDialog.visible = true
            this.rankDialog.data.shop_id = this.queryParams.shop_id
            this.rankDialog.data.createtime = this.queryParams.createtime
        },
        UnmatchedDispute() {
            this.UnmatchedDialog.visible = true
            this.UnmatchedDialog.data.tabstatus = this.queryParams.tabstatus
        },

        handleStatus(tab) {
            this.pageInfo.page = 1
            this.queryParams.tabstatus = tab.name
            this.getList()
        },
        toSupply(url) {
            window.open(url, '_blank')
        },
        handleClick() {
            this.getList()
        },
        downloadExcel() {
            window.open(this.downloadUrl, '_blank')
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.date?.length == 2) {
                qyparams.date = [parseTime(qyparams.date[0], 'y-m'), parseTime(qyparams.date[1], 'y-m')]
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            exportCheckDispute(qyparams)
                .then(res => {
                    console.log(res)
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    console.log(err)
                    this.download = false
                })
        },
        showDetailDialog(row, type) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
            this.detailDialog.type = type
        },
        ReasonForDisputeDialog(row, type) {
            this.disputeDialog.visible = true
            this.disputeDialog.data = row
            this.disputeDialog.type = type
        },
        showProductNewAuditDialog(row) {
            this.auditDialog.visible = true
            this.auditDialog.data = row
        },
        changeCustomDate() {
            this.queryParams.date = []
            this.handleQuery()
        },

        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.date?.length == 2) {
                qyparams.date = [parseTime(qyparams.date[0], 'y-m'), parseTime(qyparams.date[1], 'y-m')]
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            queryCheckDisputeList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                    this.spends_Total = response.data.spends_Total
                    this.spends = response.data?.spends ?? []
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
        showConversaDialog(row, type) {
            this.conversaDialog.visible = true
            this.conversaDialog.data = row
            this.conversaDialog.type = type
        },
    },
}
</script>
<style scoped>
.border-cell {
    padding: 0 !important;
}
.border-cell .cell {
    padding: 0 !important;
}
.border-cell-item {
    padding: 10px;
    border-bottom: 1px solid #dfe6ec;
}
.border-cell-item:last-child {
    border-bottom: 0;
}
.el-tabs--border-card {
    box-shadow: none;
    border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.table-wrap {
    padding: 15px;
    border-bottom: 1px solid #dfe4ed;
    border-left: 1px solid #dfe4ed;
    border-right: 1px solid #dfe4ed;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
</style>
